import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
const { name } = require('./package.json')
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import zipPack from 'vite-plugin-zip-pack'
const buildFileName = 'dist';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    zipPack({
      inDir: buildFileName,
      outDir: '.',
      outFileName: `${buildFileName}-${name}-${Date.now()}.zip`
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
    extensions: ['.js', '.ts', 'jsx', '.tsx', '.vue', '.json', '.css', '.scss']
  },
  define: {
    __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: false // 消除__VUE_PROD_HYDRATION_MISMATCH_DETAILS__未定义警告，是否在SSR错误时打印错误信息
  },
  css: {
    preprocessorOptions: {
      scss: { api: 'modern-compiler' },
    }
  },
  build: {
    base: '/', // 静态资源目录名称
    outDir: buildFileName, // 打包生成的目录名称
    target: 'es2015', // 兼容 Chrome 75 支持 es2015
    rollupOptions: {
      output: {
        // JS 文件输出到 assets/js/
        chunkFileNames: 'assets/js/[name]-[hash].js',
        entryFileNames: 'assets/js/[name]-[hash].js',
        // 静态资源按类型分类到 assets 下不同子目录
        assetFileNames: (assetInfo) => {
          const extType = assetInfo.name.split('.').pop().toLowerCase();
          if (['png', 'jpg', 'jpeg', 'gif', 'svg'].includes(extType)) {
            return 'assets/img/[name]-[hash].[ext]';
          } else if (['woff', 'woff2', 'ttf', 'eot', 'otf'].includes(extType)) {
            return 'assets/fonts/[name]-[hash].[ext]';
          } else if (extType === 'json') {
            return 'assets/data/[name]-[hash].[ext]';
          } else if (['mp4', 'webm', 'ogg', 'mp3', 'wav', 'flac', 'aac'].includes(extType)) {
            return 'assets/media/[name]-[hash].[ext]';
          }
          return 'assets/[name]-[hash].[ext]'; // 默认归类到 assets/
        }
      }
    }
  },
  server: {
    host: '0.0.0.0',
    proxy: {
      '/api': {
        target: 'http://192.168.30.203:7701/bigdata-governance',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  }
})
